functions in C#

A function tells you a piece of code and calls it from other parts of your code. You will soon go to such situations where you have to repeat a piece of code from many places, and this is where the work comes. They are basically C #


<visibility> <return type> <name>(<parameters>)
{
	<function code>
}

To call a function, you simply write its name, an open parenthesis, then parameters, if any, and then a closing parenthesis, like this:


DoStuff();

Here is an example of our DoStuff() function:


public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}

The first part is public, visibility, and optional. If you do not define any, then the function will be more personal about it.
The type of return is the type that can be used as a valid type in C #, or as we did it, zero zero zero means that this function does not give anything to anyone. Also, this function does not take any parameters, because you can see from the empty set of brackets, so in reality this is just a tad bit boring. Let's change it:


public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}

We have changed almost everything, the function now gives an integer, there are two parameters (both integers), and instead of doing some output, it calculates and returns the result, which means that we call this code only Rather than writing the code code, you can add code to different codes. Although we do not save so much time and effort in this small example, you better believe that you will learn how to work, as you would use C #. This function is called like this:


int result = AddNumbers(10, 5);
Console.WriteLine(result);

As mentioned, in fact this work actually gives something else, because we told C # that it should be done. While declaring something else in the form of zero, we are forcing ourselves to return something. You can try to remove the return line from the example above, and see the compiler complaint:

'AddNumbers(int, int)': not all code paths return a value

The compiler is reminding us that we have a task that does not return anything, although we made the promise. And the compiler is pretty clever! Instead of removing the line, try something like this:


public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
}

You'll see the same error - but why? Because there is no guarantee that if our statement is correct and will evaluate to execute the return line, you can finally solve it by creating a return statement by default, by default:


public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
    return 0;
}

This will fix the problem created for us, and it will also show you that there may be more than one return statement in our work. As soon as one returns to the statement statement function, the function is left and no code is run in it. In this case, this means that until the result does not exceed 10, "Return 0" never arrives.